package com.hyd.project;

import java.util.ArrayList;

import java.util.List;
import com.hyd.adapter.ExpandableAdapter;
import com.hyd.adapter.ProvinceAdapter;
import com.hyd.util.AddressData;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
/**
 * 地址选择
 * @author llf
 *
 */
public class TownActivity extends Activity {
	private ExpandableListView expandableListView;
	private ListView province;
	private List<String> groupArray = new ArrayList<String>();
	private List<List<String>> childArray = new ArrayList<List<String>>();
	private ArrayList<String> provinceList = new ArrayList<String>();
	private String[] privinces;
	private String[][] cities;
	private String[][][] counties;
	private ExpandableAdapter expandableAdapter;
	private ProvinceAdapter adapter;
	private int openline;
	private String provinceStr,city,region;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_town);

		initView();
		initData();
		initClick();
	}

	private void initView() {
		expandableListView = (ExpandableListView) findViewById(R.id.expandable);
		province = (ListView)findViewById(R.id.provice);
		// 设置expandableListView默认不展开
		expandableListView.setGroupIndicator(null);
		adapter = new ProvinceAdapter(TownActivity.this, provinceList);
		province.setAdapter(adapter);
	}

	private void initData() {
		privinces = AddressData.PROVINCES;
		for (int i = 0; i < privinces.length; i++) {
			groupArray.add(privinces[i]);
		}

		cities = AddressData.CITIES;
		for (int i = 0; i < cities.length; i++) {
			List<String> tempArray = new ArrayList<String>();
			for (int j = 0; j < cities[i].length; j++) {
				tempArray.add(cities[i][j]);
			}
			childArray.add(tempArray);
		}
		
		counties = AddressData.COUNTIES;
		expandableAdapter = new ExpandableAdapter(TownActivity.this, groupArray,
				childArray);
		expandableListView.setAdapter(expandableAdapter);
	}

	private void initClick() {
		expandableListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				boolean isOpen = parent.isGroupExpanded(groupPosition);
				if (isOpen) {
					parent.collapseGroup(groupPosition);
				} else {
					parent.collapseGroup(openline);
					parent.expandGroup(groupPosition);
					openline = groupPosition;
				}
				return true;
			}
		});

		expandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				provinceList.clear();
				for (int i = 0; i < counties[groupPosition][childPosition].length; i++) {
					provinceList.add(counties[groupPosition][childPosition][i]);
				}
				provinceStr = groupArray.get(groupPosition);
				city = childArray.get(groupPosition).get(childPosition);
				adapter.notifyDataSetChanged();
				return true;
			}
		});
		
		province.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				region = provinceList.get(position);
				Uri data = Uri.parse(provinceStr + "" + city + "" + region);
				Intent intent = new Intent(null,data);
				setResult(RESULT_OK, intent);
				finish();
				overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			}
		});
	}
	
	public void back(View v){
		finish();
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
	}
}
